package com.y3k.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.thoughtworks.xstream.XStream;
import com.y3k.service.MobileQueryService;
import com.y3k.service.ServiceException;

public class MobileComunicationServlet extends HttpServlet {

	public static final Logger log = Logger.getLogger(MobileComunicationServlet.class);

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		List<Object> results = null;
		res.setContentType("text/html");

		String query = req.getParameter("query");
		MobileQueryService service = new MobileQueryService();
		try {
			log.info("[" + query + "]");
			results = service.execute(query);
		} catch (ServiceException e) {
			e.printStackTrace();
		}

		PrintWriter out = res.getWriter();
		XStream xstream = new XStream();
		xstream.alias("item", Object[].class);
		out.println(xstream.toXML(results));
	}

	public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		doGet(req, res);
	}
}